/**
 * Copyright © 2020 易久批信息技术有限公司. All rights reserved.
 */
package com.dysm.memento;

import com.alibaba.fastjson.JSON;

import java.math.BigDecimal;
import java.util.Stack;

/**
 * 备忘录模式
 * @author xiay
 * @since 2020/12/17 4:12 下午
 */
public class MementoClient {

    public static void main(String[] args) {
        Emp emp = new Emp("1001", "A", BigDecimal.valueOf(10000));

        System.out.println("第一次打印：" + JSON.toJSONString(emp));

        CareTaker careTaker = new CareTaker();
        EmpMemento memento1 = emp.memento();
        System.out.println("第一次备份");
        // 备份
        Stack<EmpMemento> empMementos = new Stack<>();
        empMementos.push(memento1);
        careTaker.setEmpMementos(empMementos);

        System.out.println("修改属性");
        emp.setName("B");
        emp.setSalary(BigDecimal.valueOf(9000));
        System.out.println("第二次打印：" + JSON.toJSONString(emp));

        EmpMemento memento2 = emp.memento();
        System.out.println("第二次备份");
        empMementos.push(memento2);

        System.out.println("修改属性");
        emp.setName("C");
        emp.setSalary(BigDecimal.valueOf(8000));
        System.out.println("第三次打印：" + JSON.toJSONString(emp));

        // 恢复
        System.out.println("恢复");
        emp.recovery(careTaker.getEmpMementos().lastElement());
        System.out.println("第三次打印：" + JSON.toJSONString(emp));

    }
}
